package br.com.massai.model.entity;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "ITEM_PLANILHA", schema = "PORTAL")
@SequenceGenerator(name = "seqItemPlanilha", sequenceName = "seq_item_planilha", allocationSize = 1)
@NamedQueries({
	@NamedQuery(name = "ItemPlanilha.findByGrupoProducao", query = "SELECT ip FROM ItemPlanilha ip WHERE ip.grupoProducao = :grupoProducao"),
	@NamedQuery(name = "ItemPlanilha.duplicata", query = "SELECT COUNT(ip) FROM ItemPlanilha ip WHERE ip.grupoProducao = :grupoProducao AND ip.itemServico = :itemServico AND ip.id <> :id")
})
public class ItemPlanilha implements Serializable {

	private static final long serialVersionUID = -2005036237747393844L;

	@Id
	@Column(name = "ID_ITEM_PLANILHA")
	@GeneratedValue(generator = "seqItemPlanilha", strategy = GenerationType.SEQUENCE)
	private Long id;

	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
	@JoinColumn(name = "ID_GRUPO_PRODUCAO", referencedColumnName = "ID_GRUPO_PRODUCAO")
	private GrupoProducao grupoProducao;

	@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
	@JoinColumn(name = "ID_ITEM_SERVICO", referencedColumnName = "ID_ITEM_SERVICO")
	private ItemServico itemServico;

	@Column(name = "VALOR", precision = 10, scale = 4, columnDefinition = "DECIMAL(10,4)")
	private BigDecimal valor;

	public ItemPlanilha() {
		valor = new BigDecimal("0");
		itemServico = new ItemServico();
		grupoProducao = new GrupoProducao();
	}
	
	public ItemPlanilha(Integer nivel, String desc){
		this();
		this.getItemServico().setNivel(nivel);
		this.getItemServico().setDescricao(desc);
	}

	@Override
	public boolean equals(Object obj) {
		if(obj == null) return false;
		if(obj instanceof ItemPlanilha)
			if(((ItemPlanilha) obj).getId().equals(this.getId())) return true;
		return false;
	}

	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public GrupoProducao getGrupoProducao() {
		return this.grupoProducao;
	}

	public void setGrupoProducao(GrupoProducao grupoProducao) {
		this.grupoProducao = grupoProducao;
	} 
	
	public ItemServico getItemServico() {
		return this.itemServico;
	}

	public void setItemServico(ItemServico itemServico) {
		this.itemServico = itemServico;
	}

	public BigDecimal getValor() {
		return valor;
	}

	public void setValor(BigDecimal valor) {
		this.valor = valor;
	}   
}
